//
//  ZHCircleView.m
//  Doctor
//
//  Created by liuzhishan on 15/12/16.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHCircleView.h"
#import "UIView+Frame.h"

#define SDProgressViewItemMargin 24
#define IMAGECIRCLEWIGHT   50

#define IMAGELESSCIRCLE   10

@interface ZHCircleView ()

@property (nonatomic,assign) CGFloat initAngel;
@property (nonatomic,assign) CGFloat firstAngle;
@property (nonatomic,assign) CGFloat secondAngle;

@property (nonatomic,retain) UIButton *blueBtn;
@property (nonatomic,retain) UIButton *pinkBtn;
@property (nonatomic,retain) UIButton *orangeBtn;



@end



@implementation ZHCircleView

-(void)setInitialAngle:(CGFloat)initAngle andFirstProportion:(CGFloat)first andSecondProportion:(CGFloat)second andThree:(CGFloat)three
{
    
    if (0 == first + second + three) {
        return;
    }
    self.initAngel = initAngle;//- M_PI_4;
    
    self.firstAngle = self.initAngel + first * M_PI * 2;
    self.secondAngle = self.firstAngle + second * M_PI * 2;
    
    [self setNeedsDisplay];
    
    [self addMyBtnsWithFirst:first andSecond:second];
    
}

-(void)addMyBtnsWithFirst:(CGFloat)first andSecond:(CGFloat)second
{
    
    [self setmyImageBtn:self.blueBtn andCenterAngle:(self.initAngel + self.firstAngle)/2.0 andProportion:first andColor:BLUEDEEPCOLOR];
    
     [self setmyImageBtn:self.orangeBtn andCenterAngle:(self.firstAngle + self.secondAngle)/2.0 andProportion:second andColor:ORANGEDEEPCOLOR];
    
    
    [self setmyImageBtn:self.pinkBtn andCenterAngle:(self.initAngel + self.secondAngle + M_PI * 2)/2.0 andProportion:(1.0-first-second) andColor:PINKDEEPCOLOR];
    
    
    
    
    
 
}

-(void)setmyImageBtn:(UIButton *)btn andCenterAngle:(CGFloat)angle andProportion:(CGFloat)proportion andColor:(UIColor *)color
{
    if ((proportion - 0.0 ) <=0.000001) return;
    
    
    CGFloat Lenght = self.width * 0.5  - IMAGELESSCIRCLE;
    btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.backgroundColor =  color;
    
    [btn setTitle:[NSString stringWithFormat:@"%.0f%%",proportion*100] forState:(UIControlStateNormal)];
    btn.titleLabel.font = [UIFont systemFontOfSize:14* WIDPro -2];
    
    CGFloat x = self.width*0.5 + Lenght * cos(angle);
    CGFloat y = self.height*0.5 + Lenght * sin(angle);
    
    btn.width = IMAGECIRCLEWIGHT;
    btn.height = btn.width;
    btn.centerX = x;
    btn.centerY = y;
    
    
    [self addSubview:btn];

    btn.layer.masksToBounds = YES;
    
    btn.layer.cornerRadius = IMAGECIRCLEWIGHT / 2;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.shouldRasterize  = YES;
        self.backgroundColor = [UIColor clearColor];
        
    }
    return self;
}

-(void)drawRect:(CGRect)rect
{

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
   //去锯齿
    
    CGContextSetAllowsAntialiasing(ctx, true);
    CGContextSetShouldAntialias(ctx, true);
    
    
    CGFloat xCenter = rect.size.width * 0.5;
    CGFloat yCenter = rect.size.height * 0.5;
    
    
    CGContextSetLineWidth(ctx,  SDProgressViewItemMargin);
    CGContextSetLineCap(ctx, kCGLineCapButt);
    
    [BLUECOLOR set];
    CGContextAddArc(ctx, xCenter, yCenter, self.width/2 - 30, self.initAngel,self.firstAngle, 0);
     CGContextStrokePath(ctx);
    
    [ORANGECOLOR set];
    CGContextAddArc(ctx, xCenter, yCenter, self.width/2 - 30, self.firstAngle, self.secondAngle, 0);
    CGContextStrokePath(ctx);
    
    [PINKCOLOR set];
    CGContextAddArc(ctx, xCenter, yCenter, self.width/2 - 30, self.secondAngle, self.initAngel, 0);
    CGContextStrokePath(ctx);


}


@end
